//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.dearbinge.utils.imge;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ImageUtil {
    public static final float DEFAULT_QUALITY = 0.2125F;

    public ImageUtil() {
    }

    public static void addWaterMark(String imgPath, String markPath, int x, int y, float alpha, String destPath) throws Exception {
        try {
            BufferedImage bufferedImage = addWaterMark(imgPath, markPath, x, y, alpha);
            ImageIO.write(bufferedImage, imageFormat(imgPath), new File(destPath));
        } catch (Exception var7) {
            throw new RuntimeException("添加图片水印异常");
        }
    }

    public static void addWaterMark(String imgPath, String markPath, int x, int y, float alpha, String format, String destPath) throws Exception {
        try {
            BufferedImage bufferedImage = addWaterMark(imgPath, markPath, x, y, alpha);
            ImageIO.write(bufferedImage, format, new File(destPath));
        } catch (Exception var8) {
            throw new RuntimeException("添加图片水印异常");
        }
    }

    public static BufferedImage addWaterMark(String imgPath, String markPath, int x, int y, float alpha) throws Exception {
        BufferedImage targetImage = null;

        try {
            Image img = ImageIO.read(new File(imgPath));
            targetImage = new BufferedImage(img.getWidth((ImageObserver)null), img.getHeight((ImageObserver)null), 1);
            Graphics2D g = targetImage.createGraphics();
            g.drawImage(img, 0, 0, (ImageObserver)null);
            Image markImg = ImageIO.read(new File(markPath));
            g.setComposite(AlphaComposite.getInstance(10, alpha));
            g.drawImage(markImg, x, y, (ImageObserver)null);
            g.dispose();
            return targetImage;
        } catch (Exception var9) {
            throw new RuntimeException("添加图片水印操作异常");
        }
    }

    public static void addTextMark(String imgPath, String text, Font font, Color color, float x, float y, float alpha, String destPath) throws Exception {
        try {
            BufferedImage bufferedImage = addTextMark(imgPath, text, font, color, x, y, alpha);
            ImageIO.write(bufferedImage, imageFormat(imgPath), new File(destPath));
        } catch (Exception var9) {
            throw new RuntimeException("图片添加文字水印异常");
        }
    }

    public static void addTextMark(String imgPath, String text, Font font, Color color, float x, float y, float alpha, String format, String destPath) throws Exception {
        try {
            BufferedImage bufferedImage = addTextMark(imgPath, text, font, color, x, y, alpha);
            ImageIO.write(bufferedImage, format, new File(destPath));
        } catch (Exception var10) {
            throw new RuntimeException("图片添加文字水印异常");
        }
    }

    public static BufferedImage addTextMark(String imgPath, String text, Font font, Color color, float x, float y, float alpha) throws Exception {
        BufferedImage targetImage = null;

        try {
            Font Dfont = font == null ? new Font("宋体", 20, 13) : font;
            Image img = ImageIO.read(new File(imgPath));
            targetImage = new BufferedImage(img.getWidth((ImageObserver)null), img.getHeight((ImageObserver)null), 1);
            Graphics2D g = targetImage.createGraphics();
            g.drawImage(img, 0, 0, (ImageObserver)null);
            g.setColor(color);
            g.setFont(Dfont);
            g.setComposite(AlphaComposite.getInstance(10, alpha));
            g.drawString(text, x, y);
            g.dispose();
            return targetImage;
        } catch (Exception var11) {
            throw new RuntimeException("添加文字水印操作异常");
        }
    }

    public static void compressImage(String imgPath, float quality, int width, int height, boolean autoSize, String destPath) throws Exception {
        try {
            BufferedImage bufferedImage = compressImage(imgPath, quality, width, height, autoSize);
            ImageIO.write(bufferedImage, imageFormat(imgPath), new File(destPath));
        } catch (Exception var7) {
            throw new RuntimeException("图片压缩异常");
        }
    }

    public static void compressImage(String imgPath, float quality, int width, int height, boolean autoSize, String format, String destPath) throws Exception {
        try {
            BufferedImage bufferedImage = compressImage(imgPath, quality, width, height, autoSize);
            ImageIO.write(bufferedImage, format, new File(destPath));
        } catch (Exception var8) {
            throw new RuntimeException("图片压缩异常");
        }
    }

    public static BufferedImage compressImage(String imgPath, float quality, int width, int height, boolean autoSize) throws Exception {
        BufferedImage targetImage = null;
        if (quality < 0.0F || quality > 1.0F) {
            quality = 0.2125F;
        }

        try {
            Image img = ImageIO.read(new File(imgPath));
            int newwidth = width > 0 ? width : img.getWidth((ImageObserver)null);
            int newheight = height > 0 ? height : img.getHeight((ImageObserver)null);
            if (autoSize) {
                double Widthrate = (double)img.getWidth((ImageObserver)null) / (double)width + 0.1D;
                double heightrate = (double)img.getHeight((ImageObserver)null) / (double)height + 0.1D;
                double rate = Widthrate > heightrate ? Widthrate : heightrate;
                newwidth = (int)((double)img.getWidth((ImageObserver)null) / rate);
                newheight = (int)((double)img.getHeight((ImageObserver)null) / rate);
            }

            targetImage = new BufferedImage(newwidth, newheight, 1);
            Graphics2D g = targetImage.createGraphics();
            g.drawImage(img, 0, 0, newwidth, newheight, (ImageObserver)null);
            g.dispose();
            return targetImage;
        } catch (Exception var15) {
            throw new RuntimeException("图片压缩操作异常");
        }
    }

    public static void imageGray(String imgPath, String destPath) throws Exception {
        imageGray(imgPath, imageFormat(imgPath), destPath);
    }

    public static void imageGray(String imgPath, String format, String destPath) throws Exception {
        try {
            BufferedImage bufferedImage = ImageIO.read(new File(imgPath));
            ColorSpace cs = ColorSpace.getInstance(1003);
            ColorConvertOp op = new ColorConvertOp(cs, (RenderingHints)null);
            bufferedImage = op.filter(bufferedImage, (BufferedImage)null);
            ImageIO.write(bufferedImage, format, new File(destPath));
        } catch (Exception var6) {
            throw new RuntimeException("图片灰白化异常");
        }
    }

    public static void imageLucency(String imgPath, String destPath) throws Exception {
        try {
            BufferedImage bufferedImage = imageLucency(imgPath);
            ImageIO.write(bufferedImage, imageFormat(imgPath), new File(destPath));
        } catch (Exception var3) {
            throw new RuntimeException("图片透明化异常");
        }
    }

    public static void imageLucency(String imgPath, String format, String destPath) throws Exception {
        try {
            BufferedImage bufferedImage = imageLucency(imgPath);
            ImageIO.write(bufferedImage, format, new File(destPath));
        } catch (Exception var4) {
            throw new RuntimeException("图片透明化异常");
        }
    }

    public static BufferedImage imageLucency(String imgPath) throws Exception {
        BufferedImage targetImage = null;

        try {
            BufferedImage img = ImageIO.read(new FileInputStream(imgPath));
            int alpha = 0;
            executeRGB(img, alpha);
            targetImage = new BufferedImage(img.getWidth((ImageObserver)null), img.getHeight((ImageObserver)null), 1);
            Graphics2D g = targetImage.createGraphics();
            g.drawImage(img, 0, 0, (ImageObserver)null);
            g.dispose();
            return targetImage;
        } catch (Exception var5) {
            throw new RuntimeException("图片透明化执行异常");
        }
    }

    public static void executeRGB(BufferedImage img, int alpha) throws Exception {
//        int rgb = false;

        for(int x = img.getMinX(); x < img.getWidth(); ++x) {
            for(int y = img.getMinY(); y < img.getHeight(); ++y) {
                int rgb = img.getRGB(x, y);
                int R = (rgb & 16711680) >> 16;
                int G = (rgb & '\uff00') >> 8;
                int B = rgb & 255;
                if (255 - R < 30 && 255 - G < 30 && 255 - B < 30) {
                    rgb = alpha + 1 << 24 | rgb & 16777215;
                    img.setRGB(x, y, rgb);
                }
            }
        }

    }

    public static void formatConvert(String imgPath, String format, String destPath) throws Exception {
        try {
            BufferedImage bufferedImage = ImageIO.read(new File(imgPath));
            ImageIO.write(bufferedImage, format, new File(destPath));
        } catch (IOException var4) {
            throw new RuntimeException("文件格式转换出错");
        }
    }

    public static void formatConvert(BufferedImage bufferedImag, String format, String destPath) throws Exception {
        try {
            ImageIO.write(bufferedImag, format, new File(destPath));
        } catch (IOException var4) {
            throw new RuntimeException("文件格式转换出错");
        }
    }

    public static String imageFormat(String imgPath) throws Exception {
        String[] filess = imgPath.split("\\\\");
        String[] formats = filess[filess.length - 1].split("\\.");
        return formats[formats.length - 1];
    }
}
